package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
        private UserService userService;
    //获取当前登录用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当SpringSecurity认证之后，会将登录的用户信息保存到上下文对象中
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);

        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }


    //用户分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    //新增用户项
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user){
       // Integer roleId = user.getRoleId();
       /* Integer id =0;*/

        try {
            userService.add(user);
           /* Integer id = user.getId();
            userService.addroles(id,roleId);*/
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
           com.itheima.pojo.User user = userService.findById(id);

            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //编辑数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User user){
        try {
            userService.edit(user);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    //删除检查项
    @PreAuthorize("hasAuthority('USER_DELETE')")//配置权限
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
}
